package cn.com.hatech.blog.common.utils;

import cn.com.hatech.blog.module.admin.dashboard.entity.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @projectName: hatech-blog
 * @package: cn.com.hatech.blog.common.utils
 * @className: UserRequest
 * @author: xuruikai
 * @description: 暂时用这种方法在提供全局用户信息
 * @date: 2020/1/11 14:27
 * @version: 1.0
 */
public class UserRequest {

    /**
     * 获取当前登录用户
     *
     * @return
     */
    public static User getCurrentUser() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return TaleUtils.getLoginUser(request);
    }


    /**
     * 获取当前登录用户ID
     *
     * @return
     */
    public static String getCurrentUserId() {
        User user = getCurrentUser();
        return user == null ? null : user.getId();
    }

    /**
     * 获取当前登录用户ip
     *
     * @return
     */
    public static String getRemoteAddr() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request.getRemoteAddr();
    }

}
